//
//  SEGRegisterViewController.m
//  SEGUhomeStandard
//
//  Created by 乔杨 on 2019/4/8.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGRegisterViewController.h"
#import "SEGShowAuthCodeViewController.h"
#import "SEGLoginViewController.h"
#import "SEGBigNavigationView.h"
#import "SEGAuthCodeView.h"
#import <Commom/UITextField+Limit.h>

@interface SEGRegisterViewController ()<SEGAuthCodeViewDelegate>

/** 大标题 */
@property (nonatomic, strong) SEGBigNavigationView *bigNavView;
/** 手机号 */
@property (nonatomic, strong) UITextField *phoneText;
/** 分割线 */
@property (nonatomic, strong) UIView *line;
/** 清除按钮 */
@property (nonatomic, strong) UIButton* cleanPhoneButton;
/** 去登录界面Lab */
@property (nonatomic, strong) SEGLabel *loginLab;
/** 获取验证码按钮 */
@property (nonatomic, strong) UIButton *getCodeBtn;
/** 验证码视图 */
@property (nonatomic, strong) SEGAuthCodeView *codeView;

@end

@implementation SEGRegisterViewController
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [[NSNotificationCenter defaultCenter] removeObserver:self.codeView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColor_W;
    self.navDisplayType = SEGNavigationBarDisplayTypeHide;
    [self.bigNavView setTitleWithString:@"注册"];
    
    [self createuUI];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.phoneText becomeFirstResponder];
    });
}

#pragma mark - UI
- (void)createuUI {
    [self.view addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bigNavView.mas_bottom).mas_offset(110);
        make.left.equalTo(self.view).mas_equalTo(30);
        make.right.equalTo(self.view).mas_offset(-30);
        make.height.mas_equalTo(0.5);
    }];
    [self.view addSubview:self.phoneText];
    [self.phoneText mas_makeConstraints:^(MASConstraintMaker* make) {
        make.bottom.equalTo(self.line.mas_top);
        make.left.equalTo(self.line.mas_left);
        make.right.equalTo(self.line.mas_right).mas_offset(-30);
        make.height.mas_equalTo(17 + 34);
    }];
    [self.view addSubview:self.cleanPhoneButton];
    [self.cleanPhoneButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.bottom.equalTo(self.line.mas_top);
        make.right.equalTo(self.line.mas_right);
        make.width.mas_equalTo(30);
        make.height.equalTo(self.phoneText);
    }];
    [self.view addSubview:self.getCodeBtn];
    [self.getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line.mas_bottom).mas_offset(160*kScaleWidth);
        make.right.equalTo(self.view.mas_right).mas_offset(-30);
        make.width.mas_equalTo(125);
        make.height.mas_equalTo(44);
    }];
    [self.view addSubview:self.loginLab];
    [self.loginLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line.mas_bottom).mas_offset(15);
        make.left.equalTo(self.line.mas_left);
        make.height.mas_equalTo(14+20);
    }];
}

#pragma mark - SEGAuthCodeViewDelegate
- (void)authCode_checkAuthCodeWithCode:(NSString *)code randomToken:(NSString *)randomToken {
    SEGShowAuthCodeViewController *vc = [SEGShowAuthCodeViewController new];
    vc.phoneStr = self.phoneText.text;
    vc.code = code;
    vc.randomToken = randomToken;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Event
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
- (void)cleanPhoneText {
    self.phoneText.text = nil;
    self.cleanPhoneButton.hidden = YES;
    self.getCodeBtn.enabled = NO;
}
- (void)getCodeButtonClick {
    self.codeView.phoneStr = self.phoneText.text;
    self.codeView.delegate = self;
    [self.codeView show];
}

#pragma mark - Getter
- (SEGBigNavigationView *)bigNavView {
    if (!_bigNavView) {
        _bigNavView = [[SEGBigNavigationView alloc] initWithType:SEGBigNavBar_backBtn];
        _bigNavView.frame = CGRectMake(0, 0, LYScreenWidth(), kBackBtn_BigNavHeight);
        _bigNavView.fontSize = 30;
        [self.view addSubview:_bigNavView];
    }
    return _bigNavView;
}
- (UITextField *)phoneText {
    if (!_phoneText) {
        _phoneText = [[UITextField alloc] init];
        [_phoneText setTextColor:kColor_1];
        [_phoneText setFont:kFont_17];
        [_phoneText setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName:kColor_C}]];
        _phoneText.clearsOnBeginEditing = NO;
        _phoneText.clearButtonMode = UITextFieldViewModeNever;
        _phoneText.keyboardType = UIKeyboardTypeNumberPad;
        [_phoneText configPhoneInputLimitWithDelegate:self];
        [_phoneText setTintColor:kColor_T];
        WeakSelf;
        _phoneText.segTextDidChangeBlock = ^(NSString *text) {
            weakSelf.cleanPhoneButton.hidden = text.length > 0 ? NO:YES;
            weakSelf.getCodeBtn.enabled = text.length >= 11 ? YES:NO;
        };
    }
    return _phoneText;
}
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = kColor_L2;
    }
    return _line;
}
- (UIButton *)cleanPhoneButton {
    if (!_cleanPhoneButton) {
        _cleanPhoneButton = [[UIButton alloc] init];
        [_cleanPhoneButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_list_delete")] forState:UIControlStateNormal];
        [_cleanPhoneButton addTarget:self
                              action:@selector(cleanPhoneText)
                    forControlEvents:UIControlEventTouchUpInside];
        _cleanPhoneButton.hidden = YES;
    }
    return _cleanPhoneButton;
}
- (UIButton *)getCodeBtn {
    if (!_getCodeBtn) {
        _getCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_getCodeBtn setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
        [_getCodeBtn setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
        [_getCodeBtn setBackgroundImage:[UIImage imageWithColor:kColor_L2] forState:UIControlStateDisabled];
        [_getCodeBtn setTitle:kGetMessageCodeTitle forState:UIControlStateNormal];
        _getCodeBtn.titleLabel.font = kFont_16;
        // 获取按钮颜色特殊处理 高亮和暗黑的时候都是黑色
        [_getCodeBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        [_getCodeBtn setTitleColor:[UIColor seg_setColorWithAny:kColor_W dark:kColor_C] forState:UIControlStateDisabled];
        _getCodeBtn.layer.masksToBounds = YES;
        _getCodeBtn.layer.cornerRadius = 22;
        [_getCodeBtn addTarget:self
                        action:@selector(getCodeButtonClick)
              forControlEvents:UIControlEventTouchUpInside];
        _getCodeBtn.enabled = NO;
    }
    return _getCodeBtn;
}
- (SEGLabel *)loginLab {
    if (!_loginLab) {
        _loginLab = [[SEGLabel alloc] init];
        NSString *str1 = @"已有账号？";
        NSString *str2 = @" 去登录";
        NSString *str = [NSString stringWithFormat:@"%@%@",str1,str2];
        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
        text.yy_font = kFont_13;
        text.yy_color = kColor_3;
        WeakSelf;
        NSRange range = [str rangeOfString:str2];
        [text yy_setTextHighlightRange:range
                                 color:kColor_S3
                       backgroundColor:[UIColor clearColor]
                             tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
                                 // 返回到登录界面
                                 for (UIViewController *vc in weakSelf.navigationController.viewControllers) {
                                     if ([vc isKindOfClass:[SEGLoginViewController class]]) {
                                         [weakSelf.navigationController popToViewController:vc animated:YES];
                                         break;
                                     }
                                 }
                             }];
        _loginLab.attributedText = text;
    }
    return _loginLab;
}
- (SEGAuthCodeView *)codeView {
    if (!_codeView) {
        _codeView = [SEGAuthCodeView new];
        _codeView.hidden = YES;
        [[UIApplication sharedApplication].delegate.window addSubview:_codeView];
    }
    return _codeView;
}

@end
